const path = require('path')
const { definePlugin, getProfile } = require('@lhy-assist/node')
const { name } = require('./package.json')

const env = require('./config')
const envConfig = env({}, getProfile(process.env))
const publicPath = envConfig.BASE_URL || '/'

function resolve(dir) {
  return path.join(__dirname, dir)
}

// vue.config.js
module.exports = {
  publicPath,
  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    client: {
      overlay: false,
    },
  },
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false,
  configureWebpack: {
    output: {
      library: `${name}-[name]`,
      libraryTarget: 'umd',
    },
    resolve: {
      fallback: {
        crypto: false,
      },
    },
  },
  chainWebpack: config => {
    definePlugin(config.plugin('define'), env, process.env)
    config.resolve.alias.set('@static', resolve('public/static'))
    config.plugin('html').tap(args => {
      args[0].buildVersion = Date.now()
      args[0].buildVersionForce = false
      return args
    })
  },
  css: {
    loaderOptions: {
      // 给 sass-loader 传递选项
      sass: {
        additionalData: `
          @forward "@/asset/css/common/_config.scss" with ($elNamespace: "${envConfig.elNamespace}", $appNamespace: "${envConfig.appName}");
          @use "@/asset/css/common/_var.scss"as *;
        `,
      },
    },
  },
  pwa: {
    workboxOptions: {
      exclude: [
        // Exclude source maps.
        /\.map$/,
        // Exclude anything starting with manifest and ending .js or .json.
        /^manifest.*\.js(?:on)?$/,
        /\.html$/,
      ],
    },
  },
}
